var articleDetailObj = {
    el: '#pageApp',
    data: {
       contentTop:'',
       artileId:null,
       artType:null,
       categoryId:'',
       prevArtileId:'',
       nextArtileId:'',
       articleDetail:{},
       articleTime:'',
       favList:[],
       commentList:[],
       artileList:[],
       hasSupport:false,
       toAddOpinion:false 
    },
    components:{

    },
    methods: {
     getTinyArticleList:function(){
       var _this = this;
       g.ajax({
         type: 'GET',
         url: g.url.format('/api/author/getTinyArticleList'),
         data:{ text:_this.searchKey, categoryId: _this.categoryId},
         success: function (result) {
  
            _this.setArticleList(result.data);  
         }
       });
     },
     setArticleList:function(data){
        var _this = this;
        for(var i = 0; i < data.length; i++){
          if(data[i].id == _this.artileId){
            _this.articleTime = data[i].time.dateFormat('yyyy-MM-dd')
            if(i - 1 >= 0){
              _this.prevArtileId = data[i-1].id
            }
            if(i + 1 <= data.length -1){
               _this.nextArtileId = data[i+1].id
            }
            break;
          }
        }
     },
     getArticle:function(){
        var _this = this;
        g.ajax({
          type: 'GET',
          url: g.url.format('/api/author/getArticle'),
          data:{ id:_this.artileId},
          success: function (result) {
             _this.setArticleDetail(result.data);  
          }
        });
     },
     setArticleDetail:function(data){
      if(this.artType == 1){
         this.articleDetail = data;
       }else if(this.artType == 2){
          data.artImg = [];
          if(data.img){
              data.imgs = data.img.split(',');
              for(var j = 0; j < data.imgs.length ; j++){
                 data.artImg.push(g.url.imgFormat(data.imgs[j]));
              }
          }
          this.articleDetail = data;
       }
     },
     getArticleCommend:function(){
        var _this = this;
        g.ajax({
          type: 'GET',
          url: g.url.format('/api/author/getArticleCommend'),
          data:{articleId:_this.artileId},
          success: function (result) {
              result.data.forEach(function (item,i) {
                item.img = g.url.imgFormat(item.img);
                item.left = 10 + 22*i +'px';
              });
               _this.favList = result.data;
          }
        });
     },
     getArticleComments:function(){
        var _this = this;
        g.ajax({
          type: 'GET',
          url: g.url.format('/api/author/getArticleComments'),
          data:{ articleId:_this.artileId},
          success: function (result) {
              result.data.forEach(function (item,i) {
                item.img = g.url.imgFormat(item.img);

              });
              _this.commentList = result.data; 
          }
        });
     },
     chageArt:function(id){
        g.window.openNew(g.url.urlHref('/page/articleDetail.html?id='+ id +'&type='+ this.artType));
     },
     toSupport:function(){
       this.hasSupport = !this.hasSupport;
       var status = 0;
       var _this = this;
       if(this.hasSupport){status = 1}
       g.ajax({
         type: 'POST',
         url: g.url.format('/api/author/setCommend'),
         data:{relationId:_this.artileId,relationType:1,status:status},
         success: function (result) {
             if(status == 1){
                g.modal.waiting({title:'已支持！',icon:'icon-dagou-shi'});
              }else{
                g.modal.waiting({title:'已取消！',icon:'icon-dagou-shi'});
              }
            _this.getArticleCommend();
         }
       });
       
     },
     addOpinion:function(){
       this.toAddOpinion = true;
       g.window.openNew(g.url.urlHref('/page/submitOption.html?id='+ this.artileId +'&type='+ this.artType));
     },
     getInitData:function(){
       this.getArticle();
       this.getTinyArticleList(); 
       this.getArticleCommend();
       this.getArticleComments();
     }
    },
    mounted:function () {
       this.artileId = g.params.get('id') || 137; 
       this.artType = g.params.get('type') || 1; 
       if(this.artType == 1){
         this.categoryId = -1;
         this.contentTop = '100px';
       }else if(this.artType == 2){
         this.categoryId = 3;
         this.contentTop = '110px';
       }
       this.getInitData();
    }
};
var initPage = function(){
  new Vue(articleDetailObj);
};